package main

import (
	"fmt"
	"math/rand"
	"sort"
	"strings"
	"time"
)

func main() {
	//arr()
	//_cap()
	//_cap_()
	//sort1()

	//pre()
	//count()
	//m()
	search()
}

func arr() {
	var testArray [3]int                        //数组会初始化为int类型的零值
	var numArray = [3]int{1, 2}                 //使用指定的初始值完成初始化
	var cityArray = [3]string{"北京", "上海", "深圳"} //使用指定的初始值完成初始化
	fmt.Println(testArray)                      //[0 0 0]
	fmt.Println(numArray)                       //[1 2 0]
	fmt.Println(cityArray)

	var testArray1 [3]int
	var numArray1 = [...]int{1, 2}
	var cityArray1 = [...]string{"北京", "上海", "深圳"}
	fmt.Println(testArray1)                          //[0 0 0]
	fmt.Println(numArray1)                           //[1 2]
	fmt.Printf("type of numArray:%T\n", numArray1)   //type of numArray:[2]int
	fmt.Println(cityArray1)                          //[北京 上海 深圳]
	fmt.Printf("type of cityArray:%T\n", cityArray1) //type of cityArray:[3]string //[北京 上海 深圳]

	a := [4][2]string{
		{"a", "b"},
		{"a", "b"},
		{"a", "b"},
		{"a", "b"},
	}

	fmt.Println(a)

	for index, value := range a {
		fmt.Println(index, value)
	}

	var b = [...]int{1, 3, 5, 7, 8}
	sum := 0
	for _, value := range b {
		sum = sum + value
	}
	fmt.Println("和为：", sum)

	//i是下标
	for i, j := range b {
		for _, y := range b[i+1:] {
			if j+y == 8 {
				fmt.Printf("(%d,%d)", j, y)
			}

			fmt.Println(j, y)
		}
	}
}
func _cap() {
	//定义切片
	//var name []T
	// 声明切片类型
	var a []string              //声明一个字符串切片
	var b = []int{}             //声明一个整型切片并初始化
	var c = []bool{false, true} //声明一个布尔切片并初始化
	//var d = []bool{false, true} //声明一个布尔切片并初始化
	fmt.Println(a)        //[]
	fmt.Println(b)        //[]
	fmt.Println(c)        //[false true]
	fmt.Println(a == nil) //true
	fmt.Println(b == nil) //false
	fmt.Println(c == nil) //false
	//fmt.Println(c == d)   //切片是引用类型，不支持直接比较，只能和nil比较

	// 基于数组定义切片
	//a := [5]int{55, 56, 57, 58, 59}
	//b := a[1:4]                     //基于数组a创建切片，包括元素a[1],a[2],a[3]
	//fmt.Println(b)                  //[56 57 58]
	//fmt.Printf("type of b:%T\n", b) //type of b:[]int

	//切片再切片

	//a1 := [...]string{"北京", "上海", "广州", "深圳", "成都", "重庆"}
	//fmt.Printf("a:%v type:%T len:%d  cap:%d\n", a1, a1, len(a1), cap(a1))
	//b1 := a1[1:3]
	//fmt.Printf("b:%v type:%T len:%d  cap:%d\n", b1, b1, len(b1), cap(b1))
	//c := b1[1:5]
	//fmt.Printf("c:%v type:%T len:%d  cap:%d\n", c, c, len(c), cap(c))
}
func _cap_() {
	var a []int
	if a == nil {
		fmt.Println("空切片")
	}
	fmt.Println(a, len(a), cap(a))

	c := make([]int, 0)
	if c == nil {
		fmt.Println("空切片")
	}
	fmt.Println(c, len(c), cap(c))
}
func sort1() {

	//a := [5]int{55, 56, 57, 58, 59}
	//b := a[:4]                     //基于数组a创建切片，包括元素a[1],a[2],a[3]
	//fmt.Println(b)                  //[56 57 58]
	//fmt.Printf("type of b:%T\n", b) //type of b:[]int

	//var a = [...] int{1,8,6,4,7,2}
	//sort2.Ints(a[:])
	//fmt.Println(a)

	a1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	b1 := make([]int, 5, 5)
	fmt.Println(b1)
	//copy(destSlice, srcSlice []T) srcSlice: 数据来源切片 destSlice: 目标切片
	copy(b1, a1)
	//b1[0] = 6
	fmt.Println(b1)
	a1 = append(a1[:2], a1[3:]...)
	fmt.Println(a1)
}
func pre() {

	a := 10
	b := &a
	fmt.Printf("%v %p \n", a, &a)
	fmt.Printf("%v %p", b, b)
}

//map
func m() {
	//scoreMap := make(map[string]int, 8)
	//scoreMap["张三"] = 90
	//scoreMap["小明"] = 100
	//fmt.Println(scoreMap)
	//fmt.Println(scoreMap["小明"])
	//fmt.Printf("type of a:%T\n", scoreMap)
	//
	//userInfo := map[string]string{
	//	"username": "沙河小王子",
	//	"password": "123456",
	//}
	//fmt.Println(userInfo) //

	scoreMap := make(map[string]int)
	scoreMap["张三"] = 90
	scoreMap["小明"] = 100
	// 如果key存在ok为true,v为对应的值；不存在ok为false,v为值类型的零值
	v, ok := scoreMap["张三"]
	if ok {
		fmt.Println(v)
	} else {
		fmt.Println("查无此人")
	}

	for k, v := range scoreMap {
		fmt.Println(k, v)
	}

}

//map 统计单词出现的次数
func count() {
	var s = "how do you do"
	var wordCount = make(map[string]int, 10)

	words := strings.Split(s, " ")

	for _, word := range words {
		v, ok := wordCount[word]
		if ok {
			wordCount[word] = v + 1
		} else {
			wordCount[word] = 1
		}
	}
	for k, v := range wordCount {
		fmt.Println(k, v)
	}

}

//按照指定顺序遍历map
func search() {

	rand.Seed(time.Now().UnixNano())

	var score = make(map[string]int, 200)

	for i := 0; i < 100; i++ {
		key := fmt.Sprint("stu%02d", i)
		value := rand.Intn(100)
		score[key] = value
	}
	//取出map中所有key存入切片keys
	var keys = make([]string, 0, 200)
	for key := range score {
		keys = append(keys, key)
	}

	//切片排序
	sort.Strings(keys)
	for _, key := range keys {
		fmt.Println(key, score[key])
	}

}
